//
//  MainTabBar.swift
//  SinaWeibo
//
//  Created by nyan on 6/12/16.
//  Copyright © 2016 nyan. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        setupUI()
    }

    override func layoutSubviews() {
       
        let w = self.bounds.width/5
        let h = self.bounds.height
        
        var index: CGFloat = 0
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        for subView in subviews {
            if subView .isKindOfClass(NSClassFromString("UITabBarButton")!) {
                print(index)
                subView.frame = CGRectOffset(rect, index * w, 0)
                index += index == 1 ? 2 : 1
            }
        }
        composeBtn.frame = CGRectOffset(rect, 2.0 * w, 0)
        
    }
    private func setupUI() {
        //添加撰写按钮
        addSubview(composeBtn)
    }

    
    lazy var composeBtn: UIButton = {
        let btn = UIButton()
        //设置按钮
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.sizeToFit()
        return btn
    }()
}
